//
//  ExamClockView.m
//  ocdemo
//
//  Created by mengchang on 2021/6/18.
//  Copyright © 2021 JIENING ZHANG. All rights reserved.
//

#import "ExamClockView.h"

@interface ExamClockView ()
@property(nonatomic, strong) UIView *hourHand;  // 时针
@property(nonatomic, strong) UIView *minHand;   // 分针
@property(nonatomic, strong) UIView *secHand;   // 秒针
@property(nonatomic, strong) UILabel *timeLable;
@property(nonatomic, strong) UILabel *angleLable;

@property(nonatomic, strong) NSDateFormatter *df;

@end

@implementation ExamClockView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        CGFloat cx = self.bounds.size.width/2;
        CGFloat cy = self.bounds.size.height/2;
        
        _timeLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 20)];
        _timeLable.text = @"00:00:00";
        _timeLable.textColor = [UIColor blackColor];
        _timeLable.font = [UIFont systemFontOfSize:16];
        [self addSubview:_timeLable];
        
        _angleLable= [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-20, self.bounds.size.width, 20)];
        _angleLable.text = @"";
        _angleLable.textColor = [UIColor blackColor];
        _angleLable.font = [UIFont systemFontOfSize:12];
        [self addSubview:_angleLable];
        
        _hourHand = [[UIView alloc]initWithFrame:CGRectMake(cx, cy-4, frame.size.width/4, 8)];
        _hourHand.backgroundColor = [UIColor redColor];
        [self addSubview:_hourHand];
        
        _minHand = [[UIView alloc]initWithFrame:CGRectMake(cx, cy-2, frame.size.width * 0.375f, 4)];
        _minHand.backgroundColor = [UIColor greenColor];
        [self addSubview:_minHand];
        
        
        _secHand = [[UIView alloc]initWithFrame:CGRectMake(cx, cy-1, frame.size.width/2, 2)];
        _secHand.backgroundColor = [UIColor blueColor];
        [self addSubview:_secHand];
        
        _df = [[NSDateFormatter alloc] init];
        _df.dateFormat = @"HH:mm:ss";
    }
    return self;
}

- (void)setClockDateTime:(NSDate *)clockDateTime {
    
    _clockDateTime = clockDateTime;
    
    NSString *timeStr = [_df stringFromDate:_clockDateTime];
    _timeLable.text = timeStr;
    
    NSArray<NSString *> *timeArr = [timeStr componentsSeparatedByString:@":"];
    
    //int secADay = 86400;
    int secHalfDay = 43200;
    int hourVal = 0;
    int minVal = 0;
    int secVal = 0;
    if([timeArr count] == 3) {
        hourVal = [(timeArr[0]) intValue];
        minVal = [(timeArr[1]) intValue];
        secVal = [(timeArr[2]) intValue];
    }
    
    int secOfTime = hourVal*3600 + minVal*60 + secVal;
    float hourAngle = (secOfTime % secHalfDay) / 43200.f;
    float minAngle = (minVal*60 + secVal) / 3600.f;
    float secAngle = secVal / 60.f;
    
    _angleLable.text = [NSString stringWithFormat:@"%.4f %.4f %.4f", hourAngle, minAngle, secAngle];
    
    hourAngle *= 2.f * M_PI;
    minAngle *= 2.f * M_PI;
    secAngle *= 2.f * M_PI;
    
    // https://kittenyang.com/anchorpoint/
    // frame.origin.x = position.x - anchorPoint.x * bounds.size.width；
    // frame.origin.y = position.y - anchorPoint.y * bounds.size.height；
    
    CGFloat cx = self.bounds.size.width/2;
    CGFloat cy = self.bounds.size.height/2;
    
    // anchorPoint 设定锚点在当前layer的位置（归一化坐标）
    // position 设定锚点在superLayer上的位置（父layer坐标）
    // view的frame 会根据 anchorPoint position 计算而改变
    _hourHand.layer.anchorPoint = CGPointMake(0, 0.5);
    _hourHand.layer.position = CGPointMake(cx, cy-(_hourHand.bounds.size.height/2.f));
    _hourHand.transform = CGAffineTransformMakeRotation(hourAngle-M_PI_2 );
    
    _minHand.layer.anchorPoint = CGPointMake(0, 0.5);
    _minHand.layer.position = CGPointMake(cx, cy-(_minHand.bounds.size.height/2.f));
    _minHand.transform = CGAffineTransformMakeRotation(minAngle-M_PI_2);
    
    _secHand.layer.anchorPoint = CGPointMake(0, 0.5);
    _secHand.layer.position = CGPointMake(cx, cy-(_secHand.bounds.size.height/2.f));
    _secHand.transform = CGAffineTransformMakeRotation(secAngle-M_PI_2);
}

@end
